<template>
  <div class="app-container">
    <div style="margin-bottom: 55px;">
      <el-row>
        <el-card shadow="never">
          <el-row style="display: flex; align-items: center;">
            <el-col :span="2">
              <el-button style="fpadding: 3px 0" type="text" icon="el-icon-arrow-left" size="small" @click="jumpBack">返回</el-button>
            </el-col>
            <el-col :span="18">
                  <el-tag type="warning" effect="dark" style="margin-right: 20px">待开发票</el-tag>
                  <strong>{{detailForm.applyNo}}</strong>
                  <el-tooltip class="item" effect="dark" content="复制" placement="top-start">
                    <i class="el-icon-copy-document" style="cursor: pointer; margin-left: 3px" @click="copyText(detailForm.applyNo)"></i>
                  </el-tooltip>
                  <el-divider direction="vertical"></el-divider>
                  <span>生成时间：{{detailForm.createTime}}</span>
                  <el-divider direction="vertical"></el-divider>
                  <span>单据状态: </span>
                    <el-link type="danger" :underline="false" v-if="detailForm.processStatus < 30" style="font-size: 16px;margin-bottom: 4px;"
                    title="发票未开，请点击开票或者修改删除"
                    >{{detailForm.processStatusName}}</el-link>
                  <el-link type="warning" :underline="false" v-if="detailForm.processStatus == 30" style="font-size: 16px;margin-bottom: 4px;"><i class="el-icon-loading"></i>{{detailForm.processStatusName}}</el-link>
                  <el-divider direction="vertical"></el-divider>
            </el-col>
            <el-col :span="6">
              <el-button type="danger" v-if="detailForm.processStatus < 30" size="mini" @click="handleDelete(detailForm.id)">删除</el-button>
              <el-button type="primary" v-if="detailForm.processStatus < 30" size="mini" @click="handleUpdate(detailForm.id)">修改</el-button>
              <el-button type="primary" v-if="detailForm.processStatus < 30" size="mini" @click="showYangZhang(detailForm.id)">样张预览</el-button>
              <el-button type="primary" v-if="detailForm.processStatus < 30" size="mini" @click="handleOpenInvoice(detailForm.id)">开票</el-button>
            </el-col>
          </el-row>
        </el-card>
      </el-row>
      <el-row style="padding-top: 10px" class="v-deep">
        <el-col :span="24" :xs="24">
          <el-collapse v-model="activeNames2">
            <el-collapse-item name="1">
              <template slot="title">
                <div>开票主体信息</div>
              </template>
              <div style="padding: 5px">
                <el-descriptions :column="4" size="small" border>
                  <el-descriptions-item label="发票类型">{{detailForm.invoiceTypeName}}</el-descriptions-item>
                  <el-descriptions-item label="结算单号">{{detailForm.orderNo}}</el-descriptions-item>
                  <el-descriptions-item label="源订单号">{{detailForm.sourceOrderNo}}</el-descriptions-item>
                  <el-descriptions-item label="是否蓝票">
                    <el-tag v-if="detailForm.invoiceFlag == 1">蓝票</el-tag>
                    <el-tag v-else type="danger">红票</el-tag>
                  </el-descriptions-item>

                  <el-descriptions-item label="发票号">{{detailForm.invoiceNo}}</el-descriptions-item>
                  <el-descriptions-item label="发票代码">{{detailForm.invoiceCode}}</el-descriptions-item>
                  <el-descriptions-item label="开票日期">{{detailForm.invoiceDate}}</el-descriptions-item>
                  <el-descriptions-item label=""></el-descriptions-item>

                  <el-descriptions-item label="单据性质">{{detailForm.natureTypeName}}</el-descriptions-item>
                  <el-descriptions-item label="处理状态">{{detailForm.processStatusName}}</el-descriptions-item>
                  <el-descriptions-item label="发票状态">{{detailForm.invoiceStatusName}}</el-descriptions-item>
                  <el-descriptions-item label="查验状态">{{detailForm.checkStatusName}}</el-descriptions-item>

                  <el-descriptions-item label="购方名称">{{detailForm.buyerName}}</el-descriptions-item>
                  <el-descriptions-item label="购方纳税识别号">{{detailForm.buyerNumber}}</el-descriptions-item>
                  <el-descriptions-item label="购方电话">{{detailForm.buyerTel}}</el-descriptions-item>
                  <el-descriptions-item label="购方地址">{{detailForm.buyerAddress}}</el-descriptions-item>
                  <el-descriptions-item label="购方开户行">{{detailForm.buyerBank}}</el-descriptions-item>
                  <el-descriptions-item label="购方账号">{{detailForm.buyerAccount}}</el-descriptions-item>
                  <el-descriptions-item label=""></el-descriptions-item>
                  <el-descriptions-item label=""></el-descriptions-item>

                  <el-descriptions-item label="销方名称">{{detailForm.sellerName}}</el-descriptions-item>
                  <el-descriptions-item label="销方纳税识别号">{{detailForm.sellerNumber}}</el-descriptions-item>
                  <el-descriptions-item label="销方电话">{{detailForm.sellerTel}}</el-descriptions-item>
                  <el-descriptions-item label="销方地址">{{detailForm.sellerAddress}}</el-descriptions-item>
                  <el-descriptions-item label="销方开户行">{{detailForm.sellerBank}}</el-descriptions-item>
                  <el-descriptions-item label="销方账号">{{detailForm.sellerAccount}}</el-descriptions-item>
                  <el-descriptions-item label=""></el-descriptions-item>
                  <el-descriptions-item label=""></el-descriptions-item>

                  <el-descriptions-item label="开票人">{{detailForm.invoiceName}}</el-descriptions-item>
                  <el-descriptions-item label="收款人">{{detailForm.collectName}}</el-descriptions-item>
                  <el-descriptions-item label="复核人">{{detailForm.reviewName}}</el-descriptions-item>
                  <el-descriptions-item label="收票邮箱">{{detailForm.collectMail}}</el-descriptions-item>

                  <el-descriptions-item label="金额(不含税)"><strong>¥{{detailForm.feeWithoutTax | formatCurrency }}</strong></el-descriptions-item>
                  <el-descriptions-item label="税额"><strong>¥{{detailForm.tax | formatCurrency}}</strong></el-descriptions-item>
                  <el-descriptions-item label="金额(含税)"><strong>¥{{detailForm.fee | formatCurrency}}</strong></el-descriptions-item>
                  <el-descriptions-item label="凭证号">{{detailForm.voucherNumber}}</el-descriptions-item>
                  <el-descriptions-item label="ERP发票流水号">{{detailForm.erpSerialNumber}}</el-descriptions-item>
                  <el-descriptions-item label="发票备注">{{detailForm.invoiceRemark}}</el-descriptions-item>
                </el-descriptions>
              </div>
            </el-collapse-item>
          </el-collapse>
        </el-col>
      </el-row>
      <el-row style="padding-top: 10px" class="v-deep">
        <el-col :span="24" :xs="24">
          <el-collapse v-model="activeNames">
            <el-collapse-item name="1" >
              <template slot="title">
                <div>商品信息</div>
              </template>
              <div style="padding: 5px" class="low-height-th-table">
                <el-table
                  border
                  stripe
                  default-expand-all
                  ref="tableRef"
                  v-loading="loading"
                  :data="invoiceEntryList"
                  :header-cell-style= "{
                    'font-size': '12px !important',
                    'font-weight': '500 !important',
                    'color':'#FFF',
                    'padding': '0px 0',
                    'background-color':'rgb(51 112 255 / 80%)',
                    textAlign: 'left',
                    'height': '30px !important'
                  }"
                  :cell-style="{ padding: '8px 10px 8px 0', textAlign: 'left' }"
                  highlight-current-row style="width: 100%;"
                  height="300px"
                >
<!--                  <el-table-column type="selection" width="55" align="center" />-->
                  <el-table-column label="序号" align="center" prop="index" width="50">
                    <template slot-scope="scope">{{scope.$index+1}}</template>
                  </el-table-column>
                  <el-table-column label="项目名称" align="left" prop="productName" />
                  <el-table-column label="规格型号" align="left" prop="specificatCode" />
                  <el-table-column label="单位" align="left" prop="unit" width="100"/>
                  <el-table-column label="数量" align="left" prop="qty" width="150"/>
                  <el-table-column label="单价(含税)" align="left" prop="price" v-if="isWithout =='含税'" :key="Math.random()">
                    <template slot-scope="scope">
                      {{scope.row.price | formatCurrency}}
                    </template>
                  </el-table-column>
                  <el-table-column label="金额(含税)" align="left" prop="amount" v-if="isWithout=='含税'" :key="Math.random()">
                    <template slot-scope="scope">
                      {{scope.row.amount | formatCurrency}}
                    </template>
                  </el-table-column>

                  <el-table-column label="单价(不含税)" align="left" prop="withoutPrice" v-if="isWithout!='含税'" :key="Math.random()">
                    <template slot-scope="scope">
                      {{scope.row.withoutPrice | formatCurrency}}
                    </template>
                  </el-table-column>
                  <el-table-column label="金额(不含税)" align="left" prop="withoutAmount" v-if="isWithout!='含税'" :key="Math.random()">
                    <template slot-scope="scope">
                      {{scope.row.withoutAmount | formatCurrency}}
                    </template>
                  </el-table-column>

                  <el-table-column label="税率" align="left" prop="taxRate" width="80" >
                    <template slot-scope="scope">
                      {{scope.row.taxRate}}%
                    </template>
                  </el-table-column>
                  <el-table-column label="税额" align="left" prop="tax" >
                    <template slot-scope="scope">
                      {{scope.row.tax | formatCurrency}}
                    </template>
                  </el-table-column>
                  <el-table-column label="订单号" align="left" prop="orderNo" />
<!--                  <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">-->
<!--                    <template slot-scope="scope">-->
<!--                    </template>-->
<!--                  </el-table-column>-->
                  <div slot="empty">
                    <svg-icon icon-class="search-none" style="font-size: 64px;" />
                    <p>暂无数据</p>
                  </div>
                </el-table>
                <div>
                  <div style="margin-top: 5px;margin-bottom: 5px;">
                    <el-radio-group v-model="isWithout" size="mini">
                      <el-radio-button label="含税"></el-radio-button>
                      <el-radio-button label="不含税"></el-radio-button>
                    </el-radio-group>
                  </div>
                </div>
              </div>
            </el-collapse-item>
          </el-collapse>
        </el-col>
      </el-row>
    </div>
    <div style="position: fixed;z-index: 999;bottom: 0px;width: 100%;margin-top: 16px;box-shadow: rgba(0, 0, 0, 0.15) 0px 2px 12px 4px;">
      <div style="display: flex;height: 56px;background-color: #FFFF;">
        <div style="width: 100px;padding: 15px 28px;background-color: #3370ff;color: #FFF;font-size: 20px;font-weight: 600;">
          <span>合计</span>
        </div>
        <div style="display: flex;flex-grow: 1;align-items: center;">
          <div style="width: 300px;padding: 0 15px;">
            <svg-icon icon-class="without-tax2" style="color: rgb(255, 88, 88);    font-size: 18px;"/> 金额(不含税)
            <span style="font-weight: 800; margin-left: 5px">
              ¥{{detailForm.feeWithoutTax | formatCurrency}}
            </span>
          </div>
          <div style="width: 300px;padding: 0 15px;">
            <svg-icon icon-class="with-tax2" style="color: rgb(255, 165, 71);    font-size: 18px;"/>  金额(含税)
            <span style="font-weight: 800; margin-left: 5px">
              ¥{{detailForm.fee | formatCurrency}}
            </span>
          </div>
          <div style="width: 300px;padding: 0 15px;">
            <svg-icon icon-class="tax"style="color: rgb(41, 180, 240);    font-size: 18px;" />  税额
            <span style="font-weight: 800; margin-left: 5px">
              ¥{{detailForm.tax | formatCurrency}}
            </span>
          </div>
          <div style="width: 250px; padding: 0 15px;">
            <el-button @click="preInvoice(detailForm.id)">上一张</el-button>
            <el-button @click="nextInvoice(detailForm.id)" type="primary">下一张</el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 查看发票 -->
    <el-dialog v-dialogDrag  :close-on-click-modal="false" title="发票样张" :visible.sync="invoiceOpen" width="1000px" append-to-body>
      <div class="dialog_box" style="height:650px;">
        <el-row>
            <el-col :span="24">
              <div style="height: 575px; background-color:#404040;">
                <div v-viewer style="visibility: hidden;height:575px">
                  <img v-for="src in images" :key="src" :src="src" :data-source="src">
                </div>
              </div>
            </el-col>
        </el-row>
      </div>
    </el-dialog>


  </div>

</template>
<style lang="scss">
  .input_num .el-input__inner{
    padding: 0 0 0 15px !important;
    color: red;
    font-weight: 700;
  }
  .el-table .warning-row {
    background: oldlace;
  }
  .el-table .success-row {
    background: #f0f9eb;
  }
  .el-card__body, .el-main {
    padding: 5px 20px !important;
  }

  .collapse-title{
    background-color: red;
  }
  .el-collapse-item__header.is-active {
    border-bottom: 1px solid #f0f2f5 !important;
  }
  .el-collapse-item__header::before{
    content: "";
    display: block;
    width: 3px;
    height: 20px;
    position: absolute;
    background-color: #297cf0;
    left: 0px;
    top: 35px;
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
  }

  .el-collapse-item__header div{
    margin-left: 10px;
  }
  .el-form-item__label{
    margin-bottom: -12px;
  }

  .v-deep{
    .el-collapse-item__arrow {
      width: 40px;
    }
    .el-icon-arrow-right:before {
      content: "展开";
      font-size: 14px;
      font-family: 'heiti';
      color: #2295ff;
    }
    .el-collapse-item__arrow.is-active {
      transform: none;
    }
    .el-collapse-item__arrow.is-active::before {
      content: "收起";
      font-size: 14px;
      font-family: 'heiti';
      color: #2295ff;
    }
    .el-form-item--mini.el-form-item, .el-form-item--small.el-form-item {
      margin-bottom: 8px;
    }
  }

  .el-descriptions__header{
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    margin-bottom: 5px !important;
    margin-top: 5px;
  }
  .el-descriptions__title::before{
    content: "";
    display: block;
    width: 3px;
    height: 20px;
    position: absolute;
    background-color: #297cf0;
    left: 0px;
    margin-top: 11px;
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
  }
  .invoice-type {
    font-size: 12px;
    font-weight: 500;
    border: 2px solid;
    padding: 2px 6px;
    color: hsl(158deg 86% 34%);
    border-radius: 4px;
  }
  .invoice-type::before,
  .invoice-type::after {
    position: absolute;
    content: '';
    width: 6px;
    height: 6px;
    background-color: #fff;
    border: 2px solid hsl(158deg 86% 34%);
    border-left-color: transparent;
    border-bottom-color: transparent;
    border-radius: 50%;
    margin-top: 10px;
  }

  .invoice-type::before {
    margin-left: -10px;
    transform: rotate(45deg);
  }

  .invoice-type::after {
    margin-left: 3px;
    transform: rotate(-135deg);
  }

</style>

<script>
    import { listInvoice, getInvoice, getNextInvoice, getPreInvoice, updateInvoice, delInvoice } from "@/api/doc/invoice";

    import Viewer from 'v-viewer';


    export default {
        name: "TodeInvoiceDetail",
        data() {
            return {
                activeNames: ['1'],
                activeNames2: ['1'],
                detailForm:{},
                loading:false,
                invoiceEntryList:[],
                isWithout:'含税',
                invoiceOpen:false,
                images:['http://localhost:8080/profile/yangzhang/yangzhang.png'],
                options:{
                    zIndex: 99999,
                    inline: true,
                    button: true,
                    navbar: true,
                    title: true,
                    toolbar: true,
                    tooltip: true,
                    movable: true,
                    zoomable: true,
                    rotatable: true,
                    scalable: true,
                    transition: true,
                    fullscreen: false,
                    keyboard: true,
                    url: 'data-source'
                }
            };
        },
        created() {
            // 获取发票ID
            let invoiceId = this.$route.params && this.$route.query.invoiceId;
            console.log("获取发票ID：" + invoiceId);
            this.init(invoiceId);
        },
        methods: {
            init(invoiceId){
                this.$modal.loading("正在初始化，请稍候...");
                getInvoice(invoiceId).then(response => {
                    this.detailForm = response.data;
                    this.invoiceEntryList = response.data.tsalInvoiceEntryList;
                    this.$modal.closeLoading();
                });
            },

            /** 跳转至详情页 */
            jumpBack(){
                //关闭当前页
                this.$store.dispatch("tagsView/delView", this.$route);
                // 需要返回列表
                this.$router.push({path:'/sal/saltobeInvoice/tobe'})
            },

            showYangZhang(invoiceId) {
                this.invoiceOpen = true;
                // TODO 获取附件

            },

            handleOpenInvoice(invoiceId) {
                this.$modal.loading("正在开票，请稍候...");
                getInvoice(invoiceId).then(response => {
                    this.detailForm = response.data;
                    this.invoiceEntryList = response.data.tsalInvoiceEntryList;
                    this.$modal.closeLoading();

                });
            },

            /** 修改按钮操作 */
            handleUpdate(invoiceId) {
                // 需要另新增一个标签页
                this.$router.push({path:'/doc/dealInvoice/tobe/update/' + invoiceId,query:{invoiceId:invoiceId, invoiceNo:'', date:new Date().getTime()}})
            },
            /** 删除按钮操作 */
            handleDelete(invoiceId) {
                const ids = invoiceId;
                this.$modal.confirm('是否确认删除,删除成功后将自动跳转回待开列表？').then(function() {
                    return delInvoice(ids);
                }).then(() => {
                    this.$modal.msgSuccess("删除成功");
                    this.jumpBack();
                }).catch(() => {});
            },

            nextInvoice(invoiceId){
                this.$modal.loading("正在初始化，请稍候...");
                getNextInvoice(invoiceId, 'tobe').then(response => {
                    if(response.data){
                        this.detailForm = response.data;
                        this.invoiceEntryList = response.data.tsalInvoiceEntryList;
                    } else {
                        this.$modal.msgWarning("没有下一个票据");
                    }
                    this.$modal.closeLoading();
                });
            },

            preInvoice(invoiceId){
                this.$modal.loading("正在初始化，请稍候...");
                getPreInvoice(invoiceId, 'tobe').then(response => {
                    if(response.data){
                        this.detailForm = response.data;
                        this.invoiceEntryList = response.data.tsalInvoiceEntryList;
                    } else {
                        this.$modal.msgWarning("没有上一个票据");
                    }
                    this.$modal.closeLoading();
                });
            },
        }
    }
</script>
